#include "main.h"

string picPath = "../pic/";
int progress = 0;
vector<vector<Point>> newContours;
vector<Vec4i> newHierarchy;
Mat sourceImg;

int main() {
    //toGrayTest();
    //doCanny();
    doFindcontrours();

    return 0;
}

void toGrayTest(){
    IplImage* sourceImg;
    IplImage* resultImg;

    string tmp = picPath + "Firefox_wallpaper.png";
    char *path = const_cast<char *>(tmp.c_str());
    sourceImg = cvLoadImage(path);
    cvShowImage("test1" , sourceImg);

    resultImg = cvCreateImage(cvGetSize(sourceImg) , IPL_DEPTH_8U , 1);
    cvCvtColor(sourceImg , resultImg , CV_RGB2GRAY);
    cvShowImage("test2" , resultImg);
    cvWaitKey(0);
}

void doCanny(){
    Mat sourceImg;
    Mat srcImg;
    Mat resultImg;
    int height , width;
    double scale = 0.5;

    sourceImg = imread(picPath + "test.jpg");
    width = sourceImg.cols * scale;
    height = sourceImg.rows * scale;
    Size scaleSize = Size(width , height);
    srcImg = Mat(scaleSize , CV_32S);
    resize(sourceImg , srcImg , scaleSize);

    cvNamedWindow("sourceTest" , CV_WINDOW_AUTOSIZE);
    cvMoveWindow("sourceTest" , 0 , 0);
    cvNamedWindow("cannyTest1" , CV_WINDOW_AUTOSIZE);
    cvMoveWindow("cannyTest1" ,width , 0);
    cvNamedWindow("cannyTest2" , CV_WINDOW_AUTOSIZE);
    cvMoveWindow("cannyTest2" ,width*2 , 0);
    cvNamedWindow("cannyTest3" , CV_WINDOW_AUTOSIZE);
    cvMoveWindow("cannyTest3" , width*3 , 0);
    cvNamedWindow("cannyTest4" , CV_WINDOW_AUTOSIZE);
    cvMoveWindow("cannyTest4" , width*4 , 0);
    cvNamedWindow("cannyTest5" , CV_WINDOW_AUTOSIZE);
    cvMoveWindow("cannyTest5" , 0 , height+50);

    //blur(srcImg , srcImg , Size(8 , 8));
    imshow("sourceTest" , srcImg);

    resultImg = Mat(scaleSize , CV_32S);
    Canny(srcImg , resultImg , 20 , 200 );
    Mat tmp = srcImg.clone();
    Mat tmp1 = srcImg.clone();
    tmp1 = Scalar::all(0);
    tmp.copyTo(tmp1 , resultImg);
    imshow("cannyTest1" , tmp1);


    resultImg = Mat(scaleSize , CV_32S);
    Canny(srcImg , resultImg , 20 , 100 );
    imshow("cannyTest2" , resultImg);

    resultImg = Mat(scaleSize , CV_32S);
    Canny(srcImg , resultImg , 1 , 300 );
    imshow("cannyTest3" , resultImg);

    resultImg = Mat(scaleSize , CV_32S);
    Canny(srcImg , resultImg , 10 , 20 );
    imshow("cannyTest4" , resultImg);

    resultImg = Mat(scaleSize , CV_32S);
    Canny(srcImg , resultImg , 40 , 400);
    imshow("cannyTest5" , resultImg);
    cvWaitKey(-1);
}

/*
 *
 * */
void doFindcontrours(){
    Mat img = imread(picPath + "ColorPic3.png");
    sourceImg = img.clone();
    imshow("test" , img);
    cvtColor(img , img , CV_BGR2GRAY);
    Mat result;
    result = img.clone();
    Canny(result , result , 100 , 80);
    threshold(result, result, 100, 255, CV_THRESH_BINARY);
    blur(result , result , Size(4 , 4));
    imshow("threshold1" , result);

    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    findContours(result,contours,hierarchy,CV_RETR_TREE, CV_CHAIN_APPROX_NONE);


//    for(int i = 0 ; i < contours.size() ; i++){
//        vector<Point> item = contours[i];
//        int size = item.size();
//        cout<<"size = "<<size<<endl;
////        if(size > 30 && size < 2000){
//        if(true){
//            newContours.push_back(item);
//            newHierarchy.push_back(hierarchy[i]);
//        }
//    }

    vector<int> re;
    re = hierarchyFilter(hierarchy);
    for(int i = 0; i < re.size(); i++){
        if(re[i] == 1)
            newContours.push_back(contours[i]);
    }


    //newContours.push_back(contours[0]);

    cout<<"存在图型数量 : "<<newContours.size()<<endl;
    //cout<<"newHierarchy size : "<<newHierarchy.size()<<endl;

    Mat contoursImage(img.rows,img.cols,CV_8U,Scalar(0));
    cvtColor(img , img , CV_GRAY2BGR);
    //drawContours(img, newContours, -1, Scalar(0 , 0 , 255) , 2);
    namedWindow("findContours", CV_WINDOW_AUTOSIZE);
    createTrackbar(" progress:", "findContours", &progress, newContours.size(), thresh_callback);
    thresh_callback(progress , 0);
    waitKey(0);
}

void thresh_callback(int,void*){
    cout<<"progress : "<<progress<<endl;
    vector<vector<Point> > contours;
    for(int i = 0; i < progress; i++){
        contours.push_back(newContours[i]);
    }
    Mat img = sourceImg.clone();
    drawContours(img, contours, -1, Scalar(0 , 0 , 0) , 2);
    imshow("findContours" , img);
}

vector<int> hierarchyFilter(vector<Vec4i> hierarchy){
    int size = hierarchy.size();
    vector<int> temp(size);
    for(int i = 0; i < size ; i++){
        Vec4i item = hierarchy.at(i);
        if(item[2] != -1){
            temp[item[2]] = 1;
        }
    }
    for(int i = 0; i < size ; i++){
        if(temp[i] == 1){
            Vec4i item = hierarchy[i];
            if(item[0] != -1)
                temp[item[0]] = 1;
            if(item[1] != -1)
                temp[item[1]] = 1;
        }
    }
    return temp;
}

